<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin;
use Input;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        /**
         *time 2017-12-08
         *author kowy
         *tool phpstorm
         */
        $datas = Admin::all();
        return view('admin.admin.index',compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.admin.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //进行数据验证
        $this -> validate($request,[
            'name' => 'required|max:255|unique:admins',
            'password'=>'required',
        ],[
            'name.required' => '账号需要填写',
            'name.max' => '账号名称过长',
            'name.unique' => '账号已经存在',
            'password.required' => '密码不能为空',
        ]);
        //进行数据过滤
        $name     = trim(Input::get('name'));
        $password = trim(Input::get('password'));
        $password = bcrypt($password);
        $time     = time();
        $res      = Admin::create(['name'=>$name,'password'=>$password,'adddate'=>$time]);
        if($res)
        return back() -> with(['info'=>'添加成功']);
        return back() -> with(['info'=>'添加失败']);


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id获取当前数据
        $datas = Admin::where('id',$id)->first();
        return view('admin.admin.edit',compact('datas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //进行数据验证
        $this -> validate($request,[
            'name'=>'required|max:255|unique:admins,name,'.$id,
        ],[
            'name.required' => '账号不能为空',
            'name.max' => '账号名称过长',
            'name.unique' => '账号已存在'
        ]);
        //进行数据过滤
        $name      = trim(Input::get('name'));
        $password  = trim(Input::get('password'));
        //如果没有修改密码,则执行账号更新
        if(empty($password)){
            $res   = Admin::find($id)->update(['name'=>$name]);
        }else{
            $password = bcrypt($password);
            $res   = Admin::find($id)->update(['name'=>$name,'password'=>$password]);
        }
        if($res)
        return back() -> with(['info'=>'更新成功']);
        return back() -> with(['info'=>'更新失败']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = Admin::destroy($id);
        if($res)
        return response() -> json(['status'=>'success','msg'=>'删除成功']);
        return response() -> json(['status'=>'error',  'msg'=>'删除失败']);
    }
}
